package com.qiumo.gmtool.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.qiumo.gmtool.common.RetResult;
import com.qiumo.gmtool.config.GenshinConfig;
import com.qiumo.gmtool.mail.MailUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * gm武器功能
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@SaCheckPermission("weapon")
@RequestMapping("/gm/weapon")
public class WeaponController {

    private final MailUtil mailUtil;

    private final GenshinConfig genshinConfig;

    /**
     * 获取角色列表
     */
    @GetMapping("/getAllWeapon")
    public RetResult getAllWeapon() {
        try {
            Path filePath = Paths.get(genshinConfig.keyPath, "gm", "Weapon.txt");
            List<Map<String, Object>> list;
            try (Stream<String> lines = Files.lines(filePath)) {
                list = lines.map(line -> {
                    String[] split = line.split(":");
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", split[0]);
                    map.put("name", split[1]);
                    return map;
                }).collect(Collectors.toList());
            }
            return RetResult.succ(list);
        } catch (IOException e) {
            log.error("Failed to read Avatar.txt file", e);
            return RetResult.err("获取角色列表失败");
        }
    }

    /**
     * 添加武器
     */
    @GetMapping("/addWeapon")
    public RetResult addWeapon(@RequestParam Map<String, String> params) {
        if (ObjectUtil.isEmpty(params.get("id"))) {
            return RetResult.err("请选择武器");
        }
        if (ObjectUtil.isEmpty(params.get("tplevel"))) {
            params.put("tplevel", "1");
        }
        if (ObjectUtil.isEmpty(params.get("level"))) {
            params.put("level", "1");
        }
        String zl = "equip add " + params.get("id") + " " + params.get("level") + " " + params.get("tplevel");
        return mailUtil.fastSend(zl);
    }

}
